<template>
  <div class="title">
    我的食谱
    <div class="add" @click="() => { router.push({ path: '/cookstep' }); }">
      +
    </div>
  </div>
  <van-search v-model="value" shape="round" placeholder="搜索食谱" />
  <div class="main">
    <div v-longpress="handleLongPress" @click.stop="foodDetail(item.id)" v-for="item in data" :key="item.id"
      class="item">
      <div class="pic">
        <img :src="item.image" alt="">
        <div class="time">
          <i class="iconfont icon-shijian"></i>{{ item.time }}
        </div>
      </div>
      <div class="content">
        <div class="note">
          <div class="name">{{ item.name }}</div>
          <div class="degree">{{ item.difficulty }}</div>
        </div>
        <p>{{ item.description }}</p>
      </div>
      <!-- 删除按钮 -->
      <button class="delete-btn" @click.stop="deleteCook(item.id, item.name)">删除</button>
    </div>
  </div>
</template>

<script setup>
import { getAllCooks, deleteCookById } from '@/api/index.js'
import { onMounted, ref } from 'vue';
import router from '@/router/index.js';
import { showConfirmDialog, showSuccessToast, showFailToast } from 'vant'

const data = ref({})

// 定义一个加载数据的函数
const loadData = async () => {
  const userInfoString = localStorage.getItem('userInfo');
  const userInfo = JSON.parse(userInfoString)
  const res = await getAllCooks(userInfo.id)
  if (res) {
    data.value = res.data // 更新data变量，触发视图更新
  }
}

onMounted(async () => {
  // 判断是否登录了
  // 获取用户信息
  const token = localStorage.getItem('token') || sessionStorage.getItem('token')
  if (!token) {
    router.push('/login')
    return
  }

  const userInfoString = localStorage.getItem('userInfo');
  const userInfo = JSON.parse(userInfoString)

  const res = await getAllCooks(userInfo.id)
  if (!res) {
    router.push('/login')
  }
  console.log(res);
  data.value = res.data
})


const foodDetail = async (id) => {
  router.push({ path: '/foodDetail', query: { id } })
  // router.push(`/foodDetail/${id}`)
}
const handleLongPress = () => {
  console.log('long press');
}

const deleteCook = async (id, name) => {
  showConfirmDialog({
    message:
      `确认删除 ${name} 这道菜吗`,
  })
    .then(async () => {
      // on confirm
      const res = await deleteCookById(id)
      console.log(res);
      await loadData()
      showSuccessToast(res.message)

    })
    .catch(() => {
      showFailToast(res.message)
    });
}

</script>

<style lang="less" scoped>
.title {
  position: sticky;
  display: flex;
  justify-content: space-between;
  align-items: center;
  top: 0;
  padding: 0 16px;
  box-sizing: border-box;
  height: 56px;
  background-color: #F0F1F2;
  line-height: 56px;
  font-weight: 500;
  font-size: 18px;
  color: #000000;
  z-index: 1;

  .add {
    text-align: center;
    color: #FFF;
    width: 40px;
    height: 40px;
    line-height: 40px;
    background-color: #FF6B6B;
    border-radius: 6px;
  }
}

.main {
  padding-bottom: 1.4rem;

  .item {
    margin: 16px;
    width: calc(100% - 32px);
    background: #FFFFFF;
    box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    overflow: hidden;

    .pic {
      width: 100%;
      position: relative;
      height: 160px;
      overflow: hidden;

      img {
        width: 100%;
      }

      .time {
        display: flex;
        align-items: center;
        position: absolute;
        top: 12px;
        right: 12px;
        padding: 6px 8px;
        background-color: #FFF;
        border-radius: 100px;
        font-weight: 400;
        font-size: 14px;
        color: #000000;

        i {
          color: #FF6B6B;
          margin-right: 5px;
        }

      }
    }

    .content {
      padding: 16px;

      .note {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .name {
          font-weight: 500;
          font-size: 18px;
          color: #000000;
        }

        .degree {
          font-weight: 400;
          font-size: 14px;
          color: #4ECDC4;
          padding: 8px;
          background: rgba(78, 205, 196, 0.1);
          border-radius: 100px;
        }
      }

      p {
        font-weight: 400;
        font-size: 14px;
        color: #4B5563;
        line-height: 24px;
      }
    }
  }

  /* 应用全阴影 */
  .longpress-active {
    box-shadow: 0 0 15px rgba(0, 0, 0, .5);
    position: relative;
  }

  /* 删除按钮样式 */
  .longpress-active .delete-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 80px;
    height: 80px;
    background-color: red;
    color: white;
    border: none;
    border-radius: 50%;
    padding: 0;
    cursor: pointer;
    font-size: 16px;
  }

  /* 默认情况下隐藏删除按钮 */
  .delete-btn {
    display: none;
  }
}
</style>
